home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK1.toast / Development Kits (Disc 1) / Apple Game Sprockets / More Sprocket Examples 1.0 / DroneZone Sources / DZInput.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-12  |  6.9 KB  |  307 lines  |  [TEXT/CWIE]

  1. /*
  2.  *    File:        DZInput.c
  3.  *
  4.  *    Contents:    Handles input devices.
  5.  *
  6.  *    Copyright © 1996 Apple Computer, Inc.
  7.  */
  8.  
  9. #include <TextUtils.h>
  10.  
  11. #include "InputSprocket.h"
  12.  
  13. #include "DZGame.h"
  14. #include "DZInput.h"
  15. #include "DZResource.h"
  16.  
  17. #define USE_MOUSE_AND_KEYBOARD 1
  18.  
  19. enum {
  20.     kDeviceList_COUNT        = 100
  21. };
  22.  
  23. enum {
  24.     kElement_Fire,
  25.     kElement_Pause,
  26.     kElement_ShowHUD,
  27.     kElement_ShowFPS,
  28.     kElement_Turn,
  29.     kElement_COUNT
  30. };
  31.  
  32.  
  33. static Boolean                    gInputActive = false;
  34. static ISpElementListReference    gInputEventList = NULL;
  35. static ISpElementReference        gInputElement[kElement_COUNT] = {NULL, NULL, NULL, NULL, NULL};
  36.  
  37.  
  38. /* =============================================================================
  39.  *        Input_Init (external)
  40.  *
  41.  *    Initializes the Input stuff.
  42.  * ========================================================================== */
  43. void Input_Init(
  44.     void)
  45. {
  46.     UInt32                        count;
  47.     ISpDeviceReference            deviceList[kDeviceList_COUNT];
  48.     
  49.     ISpNeed                        needs[kElement_COUNT] =
  50.     {
  51.         { "\p", kIconSuiteID_Fire,        0,    kISpElementKind_Button,        kISpElementLabel_Fire,    0    },
  52.         { "\p", kIconSuiteID_Pause,     0,    kISpElementKind_Button,        kISpElementLabel_Start,    0    },
  53.         { "\p", kIconSuiteID_ShowHUD,    0,    kISpElementKind_Button,        kISpElementLabel_None,    0    },
  54.         { "\p", kIconSuiteID_ShowFPS,    0,    kISpElementKind_Button,        kISpElementLabel_None,    0    },
  55.         { "\p",    kIconSuiteID_Turn,         0,    kISpElementKind_Movement,    kISpElementLabel_None,    0    },
  56.     };
  57.     
  58.     // Get the names for the needs
  59.     GetIndString(needs[kElement_Fire].name,        kStrListID_NeedsNames,    kElement_Fire+1);
  60.     GetIndString(needs[kElement_Pause].name,    kStrListID_NeedsNames,    kElement_Pause+1);
  61.     GetIndString(needs[kElement_ShowHUD].name,    kStrListID_NeedsNames,    kElement_ShowHUD+1);
  62.     GetIndString(needs[kElement_ShowFPS].name,    kStrListID_NeedsNames,    kElement_ShowFPS+1);
  63.     GetIndString(needs[kElement_Turn].name,        kStrListID_NeedsNames,    kElement_Turn+1);
  64.     
  65.     #if USE_MOUSE_AND_KEYBOARD
  66.         // Enable the mouse
  67.         
  68.         // NOTE: This is not the correct way to handle the list count thing.  We
  69.         // should actually call once with NULL for the device list, malloc a list of
  70.         // that size, and call again.
  71.         
  72.         ISpDevices_ExtractByClass(
  73.                 kISpDeviceClass_Mouse,
  74.                 kDeviceList_COUNT,
  75.                 &count,
  76.                 deviceList);
  77.         
  78.         if (count > kDeviceList_COUNT)
  79.         {
  80.             count = kDeviceList_COUNT;
  81.         }
  82.         
  83.         ISpDevices_Activate(
  84.                 count,
  85.                 deviceList);
  86.         
  87.         // Enable the keyboard
  88.         ISpDevices_ExtractByClass(
  89.                 kISpDeviceClass_Keyboard,
  90.                 kDeviceList_COUNT,
  91.                 &count,
  92.                 deviceList);
  93.         
  94.         if (count > kDeviceList_COUNT)
  95.         {
  96.             count = kDeviceList_COUNT;
  97.         }
  98.         
  99.         ISpDevices_Activate(
  100.                 count,
  101.                 deviceList);
  102.     #endif
  103.     
  104.     // Set our virtual elements
  105.       ISpElement_NewVirtualFromNeeds(
  106.               kElement_COUNT,
  107.               needs,
  108.               gInputElement,
  109.               0);
  110.     
  111.     // Autoconfigure our virtual elements based on our needs
  112.     ISpInit(
  113.             kElement_COUNT,
  114.             needs,
  115.             gInputElement,
  116.             ' dz ',
  117.             '????',  // could use this as versioning on the needs
  118.             0,
  119.             kSetListID,
  120.             0);
  121.       
  122.       // Build our list of elements, which we'll poll for events
  123.     ISpElementList_New(
  124.             0,
  125.             NULL,
  126.             &gInputEventList,
  127.             0);
  128.     
  129.     // Add the virtual elements one at a time so we can assign a refcon
  130.     ISpElementList_AddElements(
  131.             gInputEventList,
  132.             kInputEvent_Fire,
  133.             1,
  134.             &gInputElement[kElement_Fire]);
  135.     
  136.     ISpElementList_AddElements(
  137.             gInputEventList,
  138.             kInputEvent_Pause,
  139.             1,
  140.             &gInputElement[kElement_Pause]);
  141.     
  142.     ISpElementList_AddElements(
  143.             gInputEventList,
  144.             kInputEvent_ShowHUD,
  145.             1,
  146.             &gInputElement[kElement_ShowHUD]);
  147.     
  148.     ISpElementList_AddElements(
  149.             gInputEventList,
  150.             kInputEvent_ShowFPS,
  151.             1,
  152.             &gInputElement[kElement_ShowFPS]);
  153.     
  154.     // Start off suspended (because the game is stopped)
  155.     ISpSuspend();
  156. }
  157.  
  158.  
  159. /* =============================================================================
  160.  *        Input_Exit (external)
  161.  *
  162.  *    Prepares for exit.
  163.  * ========================================================================== */
  164. void Input_Exit(
  165.     void)
  166. {
  167.     if (gInputActive)
  168.     {
  169.         Input_Activate(false);
  170.     }
  171.     
  172.     if (gInputEventList != NULL)
  173.     {
  174.         ISpElementList_Dispose(gInputEventList);
  175.         gInputEventList = NULL;
  176.     }
  177.     
  178.      ISpStop();
  179.      
  180.     ISpElement_DisposeVirtual(kElement_COUNT, gInputElement);
  181. }
  182.  
  183.  
  184. /* =============================================================================
  185.  *        Input_Configure (external)
  186.  *
  187.  *    Show the configuration dialog.
  188.  * ========================================================================== */
  189. void Input_Configure(
  190.     void)
  191. {
  192.      ISpConfigure(NULL);
  193. }
  194.  
  195.  
  196. /* =============================================================================
  197.  *        Input_GetState (external)
  198.  *
  199.  *    This routine handles the elements that are polled.  It returns in outXTurn
  200.  *    and outYTurn the "turn" controls in the ±1 range.
  201.  * ========================================================================== */
  202. void Input_GetState(
  203.     float*                outXTurn,
  204.     float*                outYTurn)
  205. {
  206.     ISpMovementData        movementData;
  207.     float                xTurn = 0.0;
  208.     float                yTurn = 0.0;
  209.     
  210.     if (gInputActive)
  211.     {
  212.         // Get the data
  213.         ISpElement_GetComplexState(
  214.                 gInputElement[kElement_Turn],
  215.                 sizeof(ISpMovementData),
  216.                 &movementData);
  217.         
  218.         // Turn 'em into ±1 float range
  219.         xTurn = movementData.xAxis;
  220.         xTurn -= kISpAxisMiddle;
  221.         xTurn /= kISpAxisMaximum-kISpAxisMiddle;
  222.         
  223.         yTurn = movementData.yAxis;
  224.         yTurn -= kISpAxisMiddle;
  225.         yTurn /= kISpAxisMaximum-kISpAxisMiddle;
  226.     }
  227.     
  228.     // Return the values
  229.     *outXTurn = xTurn;
  230.     *outYTurn = yTurn;
  231. }
  232.  
  233.  
  234. /* =============================================================================
  235.  *        Input_GetEvent (external)
  236.  *
  237.  *    This routine handles the elements that are event-based.  It returns an
  238.  *    event code indicating which button has gone down.  Note that we ignore
  239.  *    button-up transitions.  Should be called repeatedly until it return
  240.  *    kInputEvent_None.
  241.  * ========================================================================== */
  242. TInputEvent Input_GetEvent(
  243.     void)
  244. {
  245.     OSErr                err;
  246.     TInputEvent            result = kInputEvent_None;
  247.     ISpElementEvent        event;
  248.     Boolean                gotEvent;
  249.     
  250.     if (gInputActive)
  251.     {
  252.         err = ISpElementList_GetNextEvent(gInputEventList, sizeof(event), &event, &gotEvent);
  253.         
  254.         if (err == noErr && gotEvent && event.data != 0)
  255.         {
  256.             result = (TInputEvent) event.refCon;
  257.         }
  258.     }
  259.     
  260.     return result;
  261. }
  262.  
  263.  
  264. /* =============================================================================
  265.  *        Input_Activate (external)
  266.  *
  267.  *    On deactivation, we suspend InputSprocket.  On activation we resume it and
  268.  *    flush the event queue.  We only allow activation when the game is in "Play"
  269.  *    state.
  270.  * ========================================================================== */
  271. void Input_Activate(
  272.     Boolean                inActivate)
  273. {
  274.     Boolean                doActivate;
  275.     
  276.     doActivate = inActivate;
  277.     if (doActivate && Game_GetState() != kGameState_Playing)
  278.     {
  279.         doActivate = false;
  280.     }
  281.     
  282.     if (gInputActive != doActivate)
  283.     {
  284.         gInputActive = doActivate;
  285.         
  286.          if (gInputActive)
  287.          {
  288.              #if USE_MOUSE_AND_KEYBOARD
  289.                  HideCursor();
  290.              #endif
  291.              
  292.              ISpResume();
  293.              ISpElementList_Flush(gInputEventList);
  294.          }
  295.          else
  296.          {
  297.              ISpSuspend();
  298.              
  299.              #if USE_MOUSE_AND_KEYBOARD
  300.                  ShowCursor();
  301.              #endif
  302.          }
  303.     }
  304. }
  305.  
  306.  
  307.